package ui.graphical;

import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


import logic.Logic;

public abstract class GuiBaseFrame extends JFrame implements ActionListener, MouseListener, FocusListener {

	/**
	 *  Basisklasse om het standaardmodel frame op te bouwen
	 */

	
	private static final long serialVersionUID = 1L;
	
	protected Logic logic;
	protected JTextField zoekVeld;
	protected JButton btnOpslaan;
	protected JButton btnVerwijderen;
	protected JButton btnToevoegen;
	protected JList listOverview;
	
	
	protected GuiBaseFrame(Logic logic, String titel) {
		this.logic = logic;
		createGUI(titel);
		addCustomActionListeners();
	}
	
	protected GuiBaseFrame(Logic logic, String titel, String imagepath) {
		this(logic,titel);
		setIconImage(Toolkit.getDefaultToolkit().getImage(imagepath));
	}
	
	private void addCustomActionListeners() {
		listOverview.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if(!e.getValueIsAdjusting()) {				 
					dispatchList(getSelectedObject());
				}
			}
		});
	}
	
	/**
	 * 
	 * @param object Het object op de geselecteerde regel van de lijst
	 */
	protected abstract void  dispatchList(Object object);
	
	private void createGUI(String titel)
	{
		setTitle(titel);
		getContentPane().setBackground(SystemColor.inactiveCaptionText);
		setBounds(new Rectangle(75, 75, 800, 600));
		
		setResizable(false);
		getContentPane().setLayout(null);
		
		zoekVeld = new JTextField();
		zoekVeld.setBounds(107, 11, 178, 20);
		getContentPane().add(zoekVeld);
		zoekVeld.setColumns(10);
		zoekVeld.addActionListener(this);	
		
		btnToevoegen = new JButton("Nieuw");
		btnToevoegen.setBounds(10, 223, 91, 23);
		btnToevoegen.addActionListener(this);
		getContentPane().add(btnToevoegen);
		
		btnVerwijderen = new JButton("Verwijderen");
		btnVerwijderen.setBounds(111, 223, 91, 23);
		btnVerwijderen.addActionListener(this);
		getContentPane().add(btnVerwijderen);
		
		btnOpslaan = new JButton("Wijzigingen Opslaan");
		btnOpslaan.setBounds(212, 223, 129, 23);
		btnOpslaan.addActionListener(this);
		getContentPane().add(btnOpslaan);
		
		listOverview = new JList(new DefaultListModel());
		listOverview.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		listOverview.setBounds(10, 39, 760, 176);
		getContentPane().add(listOverview);
	}
	
	protected Object getSelectedObject() {
		Object object = null;
		if(listOverview.getSelectedIndex()!=-1) {
			object = listOverview.getModel().getElementAt(listOverview.getSelectedIndex());
		}
		return object;
	}

	public void actionPerformed(ActionEvent e) {
	}
	public void mouseClicked(MouseEvent e) {
	}
	public void mouseEntered(MouseEvent e) {
	}
	public void mouseExited(MouseEvent e) {
	}
	public void mousePressed(MouseEvent e) {
	}
	public void mouseReleased(MouseEvent e) {
	}
	public void focusGained(FocusEvent e) {
	}
	public void focusLost(FocusEvent e) {
	}
}
